package com.focus.xyz.union.pages.frontend.forms;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BaseForm;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.union.data.ApplicantBean;
import com.focus.xyz.union.data.InsDataService;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.List;

/**
 * Created by chenmeng on 2016/5/11.
 */
public class ApplicantForm extends BaseForm {
    private Action action;
    private static final String NODE_XPATH = "tr/td/select|tr/td/input";
    private WebDriver driver;
    @FindBy(xpath = "//*[@id=\"dev_insappdto_table\"]/tbody")
    public WebElement applicantTable;

    /*投保人信息 Start*/
    @FindBy(id = "dev_insAppDtoName")
    private WebElement applicantName;
    @FindBy(id = "dev_insAppDtoCardType")
    private WebElement appCardType;
    @FindBy(id = "dev_insAppDtoCardNo")
    private WebElement appCardNo;
    @FindBy(id = "dev_appBirthday")
    private WebElement appBirthDate;
    @FindBy(id = "dev_insAppDtoProvince")
    private WebElement appProvince;
    @FindBy(id = "dev_insAppDtoCity")
    private WebElement appCity;
    /*投保人的性别，可能是隐藏的状态*/
    @FindBy(id = "dev_insAppDtoGender")
    private WebElement genders;
    @FindBy(id = "dev_insAppDtoMobile")
    private WebElement mobile;
    @FindBy(id = "dev_insAppDtoEmail")
    private WebElement email;
    /*投保人信息 End*/


    public ApplicantForm(WebDriver driver, Action action){
        super(driver,action);
        this.action = action;
        this.driver = driver;
        PageFactory.initElements(this.driver,this);
    }
    @Override
    protected List<WebElement> getAllElements() throws Exception {
        List<WebElement> all = applicantTable.findElements(By.xpath(NODE_XPATH));
        for(WebElement e:all){
            System.out.println("private String " +e.getAttribute(Constants.ATTR_ID)+";");
        }
        return all;
    }

    public void selectGender(String key) throws Exception{
        if(action.isElementExsit(By.xpath("//td[@id='dev_insAppDtoGender' and @class !='fn-hide']"))){
            List<WebElement> gl = genders.findElements(By.tagName(Constants.TAG_INPUT));
            if(gl!=null&&gl.size() > 0){
                if("男".equals(key)){
                    action.click(gl.get(0));
                }else if("女".equals(key)){
                    action.click(gl.get(1));
                }

            }
        }
    }

    /*填写投保人的信息*/
    public void fillApplicant(String applicantId) throws Exception{
        if(StringUtils.isNotBlank(applicantId)){
            if(StringUtils.isBlank(applicantName.getAttribute("value"))){
                //填写投保人信息
                ApplicantBean ab = InsDataService.getApplicantById(applicantId);
                bind(ab);
                selectGender(ab.getGender());
            }
        }

    }


    /*填写投保人信息*/
    public void fillApplicant1(ApplicantBean bean) throws Exception{
        if(StringUtils.isBlank(applicantName.getAttribute("value"))){
            //填写投保人信息
            action.inputText(applicantName,bean.getDev_insAppDtoName());
            String cardType = bean.getDev_insAppDtoCardType();
            action.select(appCardType,cardType);
            action.inputText(appCardNo,bean.getDev_insAppDtoCardNo());
            if(StringUtils.isNotBlank(cardType)&&!"身份证".equals(cardType)){
                //输入生日及性别
                action.inputText(appBirthDate,bean.getDev_appBirthday());
                selectGender(bean.getGender());
            }
            action.select(appProvince,bean.getDev_insAppDtoProvince());
            action.select(appCity,bean.getDev_insAppDtoCity());
            action.inputText(mobile,bean.getDev_insAppDtoMobile());
            action.inputText(email,bean.getDev_insAppDtoEmail());
        }
        if(StringUtils.isBlank(mobile.getAttribute("value"))){
            action.inputText(mobile,bean.getDev_insAppDtoMobile());
        }
    }
}
